//
//  YJDBindView.m
//  YJDSDK
//
//  Created by skyline on 2018/7/27.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "YJDBindView.h"
@interface YJDBindView ()<YJDTextFieldDelegate>

@end
@implementation YJDBindView
- (YJDButton *)titleIMG
{
    if (!_titleIMG)
    {
        _titleIMG = [[YJDButton alloc] init];
        _titleIMG.height = TITLE_HEIGHT;
        _titleIMG.style = YJDButtonTitleStyleCameo;
        _titleIMG.userInteractionEnabled = NO;
        [self addSubview:_titleIMG];
        [_titleIMG mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.centerY.equalTo(self.backBtn);
            make.size.mas_offset(CGSizeMake(70, TITLE_HEIGHT));
        }];
    }
    return _titleIMG;
}

- (YJDTextField *)phoneTF
{
    if (!_phoneTF)
    {
        _phoneTF = [[YJDTextField alloc] init];
        _phoneTF.type = YJDTextFieldTypePhoneNumber;
        _phoneTF.returnKeyType = UIReturnKeyNext;
        _phoneTF.customDelegate = self;
        [self addSubview:_phoneTF];
        [_phoneTF mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            if ([YJDHelper shareHelp].style == YJDPlatformStyleImage)
                make.top.equalTo(self).with.offset(GAP * 3.0 + BACKBUTTON_HEIGHT);
            else
                make.top.equalTo(self).with.offset(GAP * 3.0 + BACKBUTTON_HEIGHT);
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(self.bounds.size.width - GAP * 2, TEXTFILDHEIGHT));
        }];
    }
    return _phoneTF;
}

- (YJDTextField *)verifyTF
{
    if (!_verifyTF)
    {
        _verifyTF = [[YJDTextField alloc] init];
        _verifyTF.type = YJDTextFieldTypeVerify;
        _verifyTF.customDelegate = self;
        [self addSubview:_verifyTF];
        [_verifyTF mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.top.equalTo(self.phoneTF.mas_bottom).with.offset(GAP * 2.0);
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(self.bounds.size.width - GAP * 2, TEXTFILDHEIGHT));
        }];
    }
    return _verifyTF;
}

- (YJDButton *)ignoreBtn
{
    if (!_ignoreBtn)
    {
        _ignoreBtn= [[YJDButton alloc] init];
        _ignoreBtn.height = TEXTFILDHEIGHT;
        [_ignoreBtn addTarget:self action:@selector(yjd_actionForIgnoreButton) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_ignoreBtn];
        [_ignoreBtn mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(GAP);
            make.top.equalTo(self.verifyTF.mas_bottom).with.offset(GAP * 2.0);
            make.size.mas_offset(CGSizeMake((self.bounds.size.width - GAP * 3.0) / 3.0 * 2.0, BUTTONHEIGHT));
        }];
    }
    return _ignoreBtn;
}

- (YJDButton *)bindBtn
{
    if (!_bindBtn)
    {
        _bindBtn= [[YJDButton alloc] init];
        _bindBtn.height = TEXTFILDHEIGHT;
        [_bindBtn addTarget:self action:@selector(yjd_actionForBindButton) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_bindBtn];
        [_bindBtn mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.right.equalTo(self).with.offset(- GAP);
            make.centerY.equalTo(self.ignoreBtn);
            make.size.mas_offset(CGSizeMake((self.bounds.size.width - GAP * 3.0) / 3.0, BUTTONHEIGHT));
        }];
    }
    return _bindBtn;
}

- (void)yjd_actionForIgnoreButton
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(yjd_actionForYJDBindViewWithEvent:dict:)])
    {
        [self.delegate yjd_actionForYJDBindViewWithEvent:YJDBindViewEventIgnore dict:nil];
    }
}

- (void)yjd_actionForBindButton
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(yjd_actionForYJDBindViewWithEvent:dict:)])
    {
        [self.delegate yjd_actionForYJDBindViewWithEvent:YJDBindViewEventBind dict:@{@"phone":self.phoneTF.text}];
    }
}

#pragma mark - YJDTextFieldDelegate
- (BOOL)yjd_actionForVerifyCode
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(yjd_actionForYJDBindViewWithEvent:dict:)])
    {
        [self.delegate yjd_actionForYJDBindViewWithEvent:YJDBindViewEventVerify dict:@{@"phone":self.phoneTF.text}];
        return ([self.phoneTF.text yjd_isMobile])?YES:NO;
    }
    return NO;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.returnKeyType == UIReturnKeyNext)
    {
        [self.verifyTF becomeFirstResponder];
        return NO;
    }
    return YES;
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
